/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import controlador.Utilidades;
import controlador.servicios.ServicioTipoProducto;
import javax.swing.JOptionPane;
import vista.modeloTablas.ModeloTablaTipoProducto;

/**
 *
 * @author Alex
 */
public class PanelAdministrarTiposProducto extends javax.swing.JPanel {

    /**
     * Creates new form PanelAdministrarTiposProducto
     */
    private ServicioTipoProducto sm = new ServicioTipoProducto();
    private ModeloTablaTipoProducto modelo = new ModeloTablaTipoProducto();

    public PanelAdministrarTiposProducto() {
        initComponents();
        cargarTabla();
    }

    private void cargarTabla() {
        modelo.setLista(sm.listar());
        tablaListaTipoProductos.setModel(modelo);
        tablaListaTipoProductos.updateUI();
    }

    private void limpiarCampos() {
        txtNombreTipoProducto.setText("");
        sm.nuevaInstancia();
    }

    private void cargarObjeto() {
        sm.getTipoProducto().setNombre_tipo(txtNombreTipoProducto.getText());
    }

    private void cargarVista() {
        txtNombreTipoProducto.setText(sm.getTipoProducto().getNombre_tipo());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombreTipoProducto = new javax.swing.JTextField();
        btnCrearTipoProducto = new javax.swing.JButton();
        btnCancelarE1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaListaTipoProductos = new javax.swing.JTable();
        btnEliminarTipoProducto = new javax.swing.JButton();
        btnCancelarE = new javax.swing.JButton();
        btnModificarTipoProducto = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Administrar Tipos de Producto"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Crear Nuevo Tipo de Producto"));

        jLabel1.setText("Ingrese el nombre del Nuevo Tipo:");

        btnCrearTipoProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/save.png"))); // NOI18N
        btnCrearTipoProducto.setText("Crear");
        btnCrearTipoProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrearTipoProductoActionPerformed(evt);
            }
        });

        btnCancelarE1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarE1.setText("Cancelar");
        btnCancelarE1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarE1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtNombreTipoProducto))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCancelarE1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCrearTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombreTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCrearTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelarE1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Tipos de Producto"));

        tablaListaTipoProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Numero", "Nombre Tipo Producto"
            }
        ));
        jScrollPane1.setViewportView(tablaListaTipoProductos);

        btnEliminarTipoProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/delete-item.png"))); // NOI18N
        btnEliminarTipoProducto.setText("Eliminar");
        btnEliminarTipoProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarTipoProductoActionPerformed(evt);
            }
        });

        btnCancelarE.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarE.setText("Salir");

        btnModificarTipoProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/edit.png"))); // NOI18N
        btnModificarTipoProducto.setText("Modificar");
        btnModificarTipoProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarTipoProductoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnModificarTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnEliminarTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelarE, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelarE, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminarTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificarTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCrearTipoProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrearTipoProductoActionPerformed
        // TODO add your handling code here:
        if(Utilidades.requerido(txtNombreTipoProducto, txtNombreTipoProducto.getText())==true){
            cargarObjeto();
            if(sm.getTipoProducto().getId_tipo()==null){
                if(sm.guardar()==true){
                    JOptionPane.showMessageDialog(this, "Se ha guardado", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    limpiarCampos();
                    cargarTabla();
                }else{
                    JOptionPane.showMessageDialog(this, "No se ha guardado", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }else{
                if(sm.guardar()==true){
                    JOptionPane.showMessageDialog(this, "Se ha modificar", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    limpiarCampos();
                    cargarTabla();
                }else{
                    JOptionPane.showMessageDialog(this, "No se ha modificar", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnCrearTipoProductoActionPerformed

    private void btnModificarTipoProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarTipoProductoActionPerformed
        // TODO add your handling code here:
        int fila = tablaListaTipoProductos.getSelectedRow();
        if(fila >=0){
            sm.fijarInstancia(modelo.getLista().get(fila));
            cargarVista();
        }else{
            JOptionPane.showMessageDialog(this, "Escoja un dato de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarTipoProductoActionPerformed

    private void btnEliminarTipoProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarTipoProductoActionPerformed
        // TODO add your handling code here:
        int fila = tablaListaTipoProductos.getSelectedRow();
        if(fila >=0){
            sm.fijarInstancia(modelo.getLista().get(fila));
            boolean aux = (sm.getTipoProducto().isEstado_tipo()==true)?false:true;
            sm.getTipoProducto().setEstado_tipo(aux);
            sm.guardar();
            JOptionPane.showMessageDialog(this, "Se ha modificado el estado", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            limpiarCampos();
            cargarTabla();
        }else{
            JOptionPane.showMessageDialog(this, "Escoja un dato de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnEliminarTipoProductoActionPerformed

    private void btnCancelarE1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarE1ActionPerformed
        // TODO add your handling code here:
        limpiarCampos();
    }//GEN-LAST:event_btnCancelarE1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarE;
    private javax.swing.JButton btnCancelarE1;
    private javax.swing.JButton btnCrearTipoProducto;
    private javax.swing.JButton btnEliminarTipoProducto;
    private javax.swing.JButton btnModificarTipoProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaListaTipoProductos;
    private javax.swing.JTextField txtNombreTipoProducto;
    // End of variables declaration//GEN-END:variables
}
